import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../app_views.dart';

/// RootView 是底部导航容器
class RootView extends GetView<RootLogic> {
  const RootView({super.key});

  @override
  Widget build(BuildContext context) {
    final screens = [
      const ToolsView(),
      const AnalysisView(), // 命理分析页面 - Analysis page
      const ClassroomView(),
      const ProfileView(),
    ];

    return GetBuilder<RootLogic>(
        init: RootLogic(),
        builder: (controller) {
          return Scaffold(
            body: IndexedStack(
              index: controller.state.currentIndex,
              children: screens,
            ),
            bottomNavigationBar: Container(
              decoration: BoxDecoration(
                gradient: LinearGradient(
                  begin: Alignment.topCenter,
                  end: Alignment.bottomCenter,
                  colors: [
                    AppColors.backgroundCard,
                    AppColors.backgroundCard.withOpacity(0.95),
                  ],
                ),
                borderRadius: const BorderRadius.only(
                  topLeft: Radius.circular(32),
                  topRight: Radius.circular(32),
                ),
                boxShadow: const [
                  BoxShadow(
                    color: AppColors.shadow,
                    blurRadius: 20,
                    offset: Offset(0, -8),
                  ),
                ],
              ),
              child: ClipRRect(
                borderRadius: const BorderRadius.only(
                  topLeft: Radius.circular(32),
                  topRight: Radius.circular(32),
                ),
                child: BottomNavigationBar(
                  backgroundColor: Colors.transparent,
                  elevation: 0,
                  type: BottomNavigationBarType.fixed,
                  currentIndex: controller.state.currentIndex,
                  onTap: controller.onTabSelected,
                  selectedItemColor: AppColors.primary,
                  unselectedItemColor: AppColors.textSecondary,
                  selectedLabelStyle: GoogleFonts.notoSansSc(
                    fontSize: 12,
                    fontWeight: FontWeight.w700,
                  ),
                  unselectedLabelStyle: GoogleFonts.notoSansSc(
                    fontSize: 12,
                    fontWeight: FontWeight.w500,
                  ),
                  items: controller.state.navItems.asMap().entries.map(
                    (entry) {
                      final index = entry.key;
                      final item = entry.value;
                      return BottomNavigationBarItem(
                        icon: AnimatedContainer(
                          duration: const Duration(milliseconds: 300),
                          curve: Curves.easeInOut,
                          padding: const EdgeInsets.all(12),
                          decoration: BoxDecoration(
                            gradient: controller.state.currentIndex == index
                                ? const LinearGradient(
                                    begin: Alignment.topLeft,
                                    end: Alignment.bottomRight,
                                    colors: AppColors.gradientWarm,
                                  )
                                : null,
                            color: controller.state.currentIndex == index
                                ? null
                                : Colors.transparent,
                            borderRadius: BorderRadius.circular(20),
                            boxShadow: controller.state.currentIndex == index
                                ? [
                                    BoxShadow(
                                      color: AppColors.primary.withOpacity(0.3),
                                      blurRadius: 12,
                                      offset: const Offset(0, 4),
                                    ),
                                  ]
                                : null,
                          ),
                          child: Icon(
                            item.icon,
                            size: 26,
                            color: controller.state.currentIndex == index
                                ? AppColors.textOnPrimary
                                : AppColors.textSecondary,
                          ),
                        ),
                        label: item.title,
                      );
                    },
                  ).toList(),
                ),
              ),
            ),
          );
        });
  }
}
